package com.java.main.domain;

import java.io.Serializable;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;

import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.support.JpaEntityInformation;
import org.springframework.data.jpa.repository.support.SimpleJpaRepository;

public class SysBaseRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRepository<T, ID>
		implements SysBaseRepository<T, ID> {

	public SysBaseRepositoryImpl(Class<T> domainClass, EntityManager entityManager) {
		super(domainClass, entityManager);
	}

	public SysBaseRepositoryImpl(JpaEntityInformation<T, ?> entityInformation, EntityManager entityManager) {
		super(entityInformation, entityManager);
	}

	public List<T> findAll(Specification<T> spec, int offset, int maxResults) {
		return findAll(spec, offset, maxResults, null);
	}

	public List<T> findAll(Specification<T> spec, int offset, int maxResults, Sort sort) {
		TypedQuery<T> query = getQuery(spec, sort);

		if (offset < 0) {
			throw new IllegalArgumentException("Offset must not be less than zero!");
		}
		if (maxResults < 1) {
			throw new IllegalArgumentException("Max results must not be less than one!");
		}

		query.setFirstResult(offset);
		query.setMaxResults(maxResults);
		return query.getResultList();
	}

}
